var Alist = {
	firends: null,
	init: function() {
		var photoCss = "background-size: cover;-webkit-background-size: cover;"
		$ajax({
			url: "/user/detail",
			data: {_id:CurrentUser._id},
			success: function(res) {
				var groups = res.result.groups;
            	SetLS("alist_groups",groups); 
            	var friends = res.result.friends;
				var html = "";
				if (!friends||friends.length==0||friends==undefined){
                        html += '<div class="umt_10">'+
                                     '<div class="tx-c">'+ (CurrentUser.category==30?"你可通过点击搜索选项,输入你要添加的好友的手机号和昵称，将其保存到这里(*^__^*)":"You can   click “+ “   to create group chat   <br/>You can search your friend via  phone no or <br>nickname after clicking “Search” ")+'</div>'+
                                '</div>';
                    }
				for (var i=0; i<groups.length; ++i) {
				    
	                //if(groups[i].name == "未分组"){
	                    html += '<div class="ub ub-f1 ufp8" style="padding:0.5em 1em; background-color:#E9E3D7;">'+(CurrentUser.category==30?"我的好友":"My Friends")+'</div>'; 
	                //}
	                /*else{
	                     html += '<div class="ub ub-f1 ufp8" style="padding:0.5em 1em; background-color:#E9E3D7;" onclick="Alist.groupDetail(\''+ i +'\')">'+ groups[i].name +'</div>';
	                }*/
	                
	                    
	                for (var j=0; j<groups[i].friends.length; ++j) {
	                    var user = groups[i].friends[j];
	                    if (user.from == CurrentUser._id && user.status == FRIENDSTATUS.PEDDING)
							continue;
						if (user.status == FRIENDSTATUS.REFUSE || user.status == FRIENDSTATUS.REMOVE)
							continue;

	                    var ubbClass = "ubb";
	                    if (j == groups[i].friends.length-1)
	                        ubbClass = "";
	                    var content = '';
						var jumpEvent = '';
						var phone = CurrentUser.category==30?"电话:"+user.phone:"Phone:" + user.phone;
						if (user.status == FRIENDSTATUS.PEDDING){
							content =   '<div class="ub upl_p5">'+
								        '    <div class="ub ub-f1 ub-ac ub-pe" ontouchstart="zy_touch()" onclick="Alist.setStatus(\''+ i +'\', \''+ j +'\', \''+ FRIENDSTATUS.AGREE +'\')"><button class="upp3 ufp8 bc_green t-wh uc-a">'+CurrentUser.category==30?"接受":"Accept"+'</button></div>'+
								        '    <div class="ub ub-f1 ub-ac ub-pe upl_p5" ontouchstart="zy_touch()" onclick="Alist.setStatus(\''+ i +'\', \''+ j +'\', \''+ FRIENDSTATUS.REFUSE +'\')"><button class="upp3 ufp8 uc-a">'+CurrentUser.category==30?"拒绝":"Reject"+'</button></div>'+
								        '</div>';
							tip = user.tip;
						} else if (user.status == FRIENDSTATUS.AGREE) {
							jumpEvent = "Alist.detail("+ i + "," + j +")";
						}
						
						html += '<div class="ub up_list3 detailEvt" ontouchstart="zy_touch()" onclick=' + jumpEvent + '>'+
						        '    <div class="uw2 uh2 uc-ap50 ub_img_cover detailEvt" style="background-image:url('+ Core.id2url(user) +')"></div>'+
						        '    <div class="ub-f1 ub ub-ver upl_p5">'+
						        '        <div class="ub-f1 ub">'+
						        '            <div class="ub-f1 ub_left">'+ user.name +'　'+ (user.sex?user.sex:'') + ' '+ (user.country?user.country:'') +'</div>'+
						        '        </div>'+
						        '        <div class="ub_left ub-f1">'+ phone+'</div>'+
						        '    </div>'+ 
						        content + 
						        '</div>';
	             	}
	            }
				$("#ctn").html(html);
			}
		});
	},
	groupDetail: function(pos){
	    
        var groups = GetLS("alist_groups");
        var alist_gdetail = groups[pos];
        SetLS("alist_gdetail", alist_gdetail);
        OpenNewWin("group_detail");
    },	
	setStatus: function(i,j,status){
		var groups = GetLS("alist_groups");
		var item = groups[i].friends[j];
		var $this = $(event.currentTarget);
		$ajax({
			url: "/user/updateFriend",
			data: {
				_id: CurrentUser._id,
				friendId: item.id,
				status: status
			},
			success: function(res){
				if (status == FRIENDSTATUS.AGREE){
					$this.parent().parent(".detailEvt").attr("onclick","Alist.detail("+ i + "," + j +")");
					$this.parent().remove();
				}
				else if (status == FRIENDSTATUS.REFUSE)
					$this.parent().parent().remove();
			}
		});
	},
	detail: function(i,j) {
		var groups = GetLS("alist_groups");
		console.log(i, j)
		console.log(groups[i])
		var item = groups[i].friends[j];
		SetLS("address_detail", item);
		OpenNewWin("address_detail");
	},
	detailInit: function() {
		var friend = GetLS("address_detail");
		$("#photo").css("background-image", "url("+ Core.id2url(friend) +")");
		$("#name").text(friend.name +"　"+ (friend.sex?friend.sex:""));
		$("#phone").text(CurrentUser.category==30?"电话:"+friend.phone:"Phone:"+friend.phone);
		var wu=CurrentUser.category==30?"无":"No";
		$("#country").text(CurrentUser.category==30?"国家:"+ (friend.courntry?friend.courntry:wu):"Country:"+ (friend.courntry?friend.courntry:wu));
		$("#education").text(CurrentUser.category==30?"学历:"+ (friend.education?friend.education:wu):"Education:"+ (friend.education?friend.education:wu));
		$("#des").text(CurrentUser.category==30?"描述:"+ (friend.des?friend.des:wu):"Description:"+ (friend.des?friend.des:wu));
	},
	chat: function() {
        SetLS("alist_chat", true);
		SetLS("chat_detail", GetLS("address_detail"));
		OpenNewWin("chat_detail");
	},	
	remove: function() {
  		//$confirm(CurrentUser.category==30?"确定要删除吗？":"Do you want to delete?",function(){
  		  uexWindow.confirm(CurrentUser.category==30?"警告":"Warning",CurrentUser.category==30?"确定要删除吗？":"Do you want to detete?",CurrentUser.category==30?"确定,取消":"Sure,Cancel");
  		  function cbConfirm(opId, dataType, ensure){
  		    if(ensure == 0){
  			var friend = GetLS("address_detail");
  			var length = CurrentUser.friends.length;
  			$ajax({
				url: "/user/delFriend",
				data: {
					_id: CurrentUser._id,
					friendId: friend.id,
					status: FRIENDSTATUS.REMOVE
				},
				success: function(res) {
				    var user = res.result[0];
					SetLS("user",user);
					var ml=CurrentUser.category==30?"../ch/":"../en/";
					PCall(ml+"address", "CCall('Alist.init();')");
					WinClose();
				}
			});
  		}
  		}
  		uexWindow.cbConfirm = cbConfirm;
	},
}